package com.common.baselibrary.rxjava2;

import android.content.Context;
import android.util.Log;

import com.common.baselibrary.progress.ProgressDialogHandler;
import com.common.baselibrary.request.ResultException;
import com.common.baselibrary.util.ToastUtil;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLHandshakeException;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public class BaseObserver<T> implements Observer<T> {

    private ResponseCallBack responseCallBack;
    //    private ProgressListener progressListener;
    private Disposable disposable;
    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    private ProgressDialogHandler mProgressDialogHandler;
    private Context context;

 /*   public BaseObserver(Context context, boolean isShowProgress, ResponseCallBack responseCallBack, ProgressListener progressListener) {
        this.responseCallBack = responseCallBack;
        this.progressListener = progressListener;
        this.context = context;
        if (isShowProgress) {//是否显示进度框
            mProgressDialogHandler = new ProgressDialogHandler(context);
        }
    } */
    public BaseObserver(Context context, boolean isShowProgress, ResponseCallBack responseCallBack) {
        this.responseCallBack = responseCallBack;
        this.context = context;
        if (isShowProgress) {//是否显示进度框
            mProgressDialogHandler = new ProgressDialogHandler(context);
        }
    }


    @Override
    public void onSubscribe(Disposable d) {
        this.disposable = d;
        showProgressDialog();
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.add(disposable);
        }
    }

    @Override
    public void onNext(T t) {
        responseCallBack.onSuccess(t);
    }

    /**
     * 如果后台返回的数据格式  data格式固定 而不是一会{} 一会[] 如果固定data的返回格式 可以使用这种方式
     */
//    @Override
//    public void onNext(BaseCallModel<T> tBaseCallModel) {
//        if (tBaseCallModel.isSuccess()) {
//            responseCallBack.onSuccess(tBaseCallModel.getData());
//        } else {
//            responseCallBack.onFault(tBaseCallModel.getMsg());
//            ToastUtil.showShortToast(tBaseCallModel.getMsg());
//        }
//    }
    @Override
    public void onError(Throwable e) {
        try {
            if (e instanceof SocketTimeoutException) {//请求超时
                responseCallBack.onFault("请求超时,请稍后再试");
                ToastUtil.showShortToast("请求超时,请稍后再试");
            } else if (e instanceof ConnectException) {//网络连接超时
                responseCallBack.onFault("网络连接超时,请检查网络状态");
                ToastUtil.showShortToast("网络连接超时,请检查网络状态");
            } else if (e instanceof SSLHandshakeException) {//安全证书异常
                responseCallBack.onFault("安全证书异常");
                ToastUtil.showShortToast("安全证书异常");
            } else if (e instanceof HttpException) {//请求的地址不存在
                int code = ((HttpException) e).code();
                if (code == 504) {
                    responseCallBack.onFault("网络异常，请检查您的网络状态");
                    ToastUtil.showShortToast("网络异常，请检查您的网络状态");
                } else if (code == 404) {
                    responseCallBack.onFault("请求的地址不存在");
                    ToastUtil.showShortToast("请求的地址不存在");
                } else {
                    responseCallBack.onFault("请求失败");
                    ToastUtil.showShortToast("请求失败");
                }
            } else if (e instanceof UnknownHostException) {//域名解析失败
                responseCallBack.onFault("域名解析失败");
                ToastUtil.showShortToast("域名解析失败");
            } else if (e instanceof ResultException) {//服务器返回的非正常状态码
                ToastUtil.showShortToast(((ResultException) e).getErrMsg());
                responseCallBack.onFault(((ResultException) e).getErrMsg());
            } else {
                responseCallBack.onFault("error:" + e.getMessage());
                ToastUtil.showShortToast(e.getMessage());
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        } finally {
            Log.e("ysm==", "error:" + e.getMessage());
            destroySubscribe();
            dismissProgressDialog();
        }

    }

    @Override
    public void onComplete() {
        dismissProgressDialog();
        destroySubscribe();
    }

    /**
     * 销毁订阅 释放空间
     */
    private void destroySubscribe() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear(); // clear时网络请求会随即cancel
            mCompositeDisposable = null;
        }
    }

    private void showProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }
}
